package com.wn.sddfp.access.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wn.sddfp.access.entity.TradeLog;
import com.wn.sddfp.access.service.ITradeLogService;
import com.wn.sddfp.common.model.dto.bank.BankCardAddDTO;
import com.wn.sddfp.common.model.dto.bank.BankCardRequestDto;
import com.wn.sddfp.common.model.dto.bank.BaseResult;
import com.wn.sddfp.common.model.dto.bank.SignatureDto;
import com.wn.sddfp.common.model.dto.borrow.BorrowSyncRecordVo;
import com.wn.sddfp.common.model.exception.BusinessException;
import com.wn.sddfp.common.model.result.Result;
import com.wn.sddfp.common.model.result.enums.impl.ResultCode;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2024-07-31
 */
@Slf4j
@RestController
@RequestMapping("/trade-log")
public class TradeLogController {

    @Autowired
    private ITradeLogService tradeLogService;

    /**
     * 绑卡
     */
    @PostMapping("/bank/access/card")
    public SignatureDto proxyBindCardLog(@RequestBody BankCardAddDTO bankCardAddDTO) {
        SignatureDto signatureDto = tradeLogService.proxyBindCardLog(bankCardAddDTO);
        return signatureDto;
    }

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/bank/access/test01/{id}")
    public Result test01(@PathVariable("id") Integer id) {
        System.out.println("id = " + id);
//        try {
//            Thread.sleep(4000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        if (id < 10) {
            throw new BusinessException(ResultCode.PARAM_IS_INVALID);
        }
        String code = RandomUtil.randomNumbers(3);
        return Result.ok(code + ":" + port);
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 银行托管系统回调接口
     */
    @PostMapping("/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDto bankCardRequestDto) {
        //01 更新日志
        // 根据流水号，修改交易记录的同步状态:synchronize_status
        tradeLogService.update(Wrappers.lambdaUpdate(TradeLog.class)
                .set(TradeLog::getSynchronizeStatus,1)  //托管平台的日志同步
                .eq(TradeLog::getReqCode,bankCardRequestDto.getReqCode())
        );

        //02 发送数据到 system系统中
        // 发送MQ消息，通知系统管理服务绑卡结果
        rabbitTemplate.convertAndSend(
                "sddfp-exchange-bind",
                "bind-card-key", //bind-card-message
                bankCardRequestDto);

        return "success";
    }

    /**
     * 借款记录审核，同步借款数据到银行托管系统
     */
    @ApiOperation("借款记录审核，同步数据")
    @PostMapping("/bank/access/sync/borrow")
    public Result syncBorrowRecord(@RequestBody BorrowSyncRecordVo borrowSyncRecordVo) {
        log.info("trade 平台 发送数据:{}",borrowSyncRecordVo);
        //BaseResult 这个结果是银行返回的结果
        BaseResult baseResult = tradeLogService.syncBorrowRecord(borrowSyncRecordVo);
        return Result.ok(baseResult);
    }
}
